<template>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-alert type="warning" title="温馨提示：用户资金变动记录" :closable="false" show-icon></el-alert>
			<el-form class="mb-[-16px] mt-[16px]" :model="queryParams" inline label-width="auto">
				<el-form-item label="用户信息" prop="user_id">
					<el-input class="w-[150px]" v-model="queryParams.user_info" clearable placeholder="账号/邀请码" />
				</el-form-item>
				<el-form-item label="订单编号" prop="sn">
					<el-input class="w-[150px]" v-model="queryParams.sn" clearable placeholder="请输入订单编号" />
				</el-form-item>
				<el-form-item label="变动类型" prop="change_type">
					<el-select class="w-[150px]" v-model="queryParams.change_type" clearable placeholder="请选择变动类型">
						<el-option label="全部" value=""></el-option>
						<el-option v-for="(item, index) in dictData.finance_type" :key="index" :label="item.name"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="冻结状态" prop="frozen">
					<el-select class="w-[150px]" v-model="queryParams.frozen" clearable placeholder="请选择冻结状态">
						<el-option label="全部" value=""></el-option>
						<el-option label="未冻结" value="0"></el-option>
						<el-option label="冻结中" value="1"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="创建时间" prop="create_time">
					<daterange-picker v-model:startTime="queryParams.start_time"
						v-model:endTime="queryParams.end_time" />
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<el-button v-perms="['finance.user_finance/delete']" :disabled="!selectData.length"
				@click="handleDelete(selectData)">
				删除
			</el-button>
			<div class="mt-4">
				<el-table :data="pager.lists" @selection-change="handleSelectionChange">
					<el-table-column type="selection" min-width="55" />
					<el-table-column label="用户" min-width="220">
						<template #default="{ row }">
							<div>账号：{{row.mobile}}</div>
							<div>邀请码：{{row.u_sn}}</div>
						</template>
					</el-table-column>
					<el-table-column label="用户层级" min-width="160" show-tooltip-when-overflow>
						<template #default="{ row }">
							<div>上级：{{row.team_top.prev_account}}</div>
							<div>一代：{{row.team_top.top_account}}</div>
							<div>代数：{{row.team_top.level}}</div>
						</template>
					</el-table-column>
					<el-table-column label="订单编号" prop="sn" min-width="130" />
					<el-table-column label="变动类型" prop="change_type" min-width="120" show-overflow-tooltip>
						<template #default="{ row }">
							<dict-value :options="dictData.finance_type" :value="row.change_type" />
							<div v-if="row.remark!=''" class="text-[#fa3534] truncate">备注：{{row.remark}}</div>
						</template>
					</el-table-column>
					<el-table-column label="变动金额" min-width="100">
						<template #default="{ row }">
							<div>{{row.action==1?'+':'-'}}{{row.change_amount}}</div>
						</template>
					</el-table-column>
					<el-table-column label="变动前金额" prop="left_amount" min-width="100" />
					<el-table-column label="来源单号" prop="source_sn" min-width="120" />
					<el-table-column label="创建时间" prop="create_time" min-width="120">
						<template #default="{ row }">
							<div>{{ row.create_time ? timeFormat(row.create_time, 'yyyy-mm-dd hh:MM:ss') : '' }}</div>
							<div class="text-success" v-if="row.frozen">
								<div>
									解冻时间
								</div>
								<div>{{ row.thaw_time ? timeFormat(row.thaw_time, 'yyyy-mm-dd hh:MM:ss') : '' }}</div>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="操作" min-width="60" fixed="right">
						<template #default="{ row }">
							<div class="mb-2">
								<el-button v-if="row.frozen == 1" v-perms="['finance.user_finance/unfrozen']" type="primary"
									link @click="handleUnfrozen(row.id)">
									手动解冻
								</el-button>
							</div>
							<div>
								<el-button v-perms="['finance.user_finance/delete']" type="danger" link
									@click="handleDelete(row.id)">
									删除
								</el-button>
							</div>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
	</div>
</template>

<script lang="ts" setup name="userFinanceLists">
	import { usePaging } from '@/hooks/usePaging'
	import { useDictData } from '@/hooks/useDictOptions'
	import { userFinanceLists, userFinanceDelete,userFinanceUnfrozen } from '@/api/finance'
	import { timeFormat } from '@/utils/util'
	import feedback from '@/utils/feedback'


	// 查询条件
	const queryParams = reactive({
		sn: '',
		user_info: '',
		change_type: '',
		frozen:'',
		action: '',
		create_time: '',
		start_time: '',
		end_time: '',
		parent_sn: '',
		level: ''
	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('finance_type')

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: userFinanceLists,
		params: queryParams
	})

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await userFinanceDelete({ id })
		getLists()
	}
	
	// 解冻
	const handleUnfrozen = async (id : number | any[]) => {
		await feedback.confirm('确定要解冻吗？')
		await userFinanceUnfrozen({ id })
		getLists()
	}
	
	getLists()
</script>